﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;

namespace IHISCETeim.DAL
{
    public class XZKaoQinDAL
    {
        public static int Add(List<XZKaoQinInfo> objs)
        {
            //Database DB = DatabaseFactory.CreateDatabase();

            //DbCommand dbCommand = DB.GetStoredProcCommand("XZKaoQin_Add", obj.GongHao, obj.RealName, obj.KQDate, obj.KQType, obj.KQValue, obj.Remark,obj.KQState);
            //return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
            ///

            Database DB = DatabaseFactory.CreateDatabase();
            using (DbConnection conn = DB.CreateConnection())
            {
                //打开连接
                conn.Open();
                //创建事务
                DbTransaction trans = conn.BeginTransaction();
                try
                {
                    foreach (XZKaoQinInfo obj in objs)
                    {
                        DbCommand dbCommand = DB.GetStoredProcCommand("XZKaoQin_Add", obj.GongHao, obj.RealName, obj.KQDate, obj.KQType, obj.KQValue, obj.Remark, obj.KQState);
                        DB.ExecuteScalar(dbCommand, trans);
                    }
                    trans.Commit();
                    return 1;
                }
                catch
                {
                    //发生异常，事务回滚
                    trans.Rollback();
                    return 0;
                    //return exc.Message;
                }
                finally
                {
                    //关闭连接
                    conn.Close();
                }
            }
        }
        public static bool Delete(int KQID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("XZKaoQin_Delete", KQID);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static DataTable GetPagination(string WhereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("XZKaoQin_GetPagination");
            DB.AddInParameter(dbCommand, "WhereSql", DbType.String, WhereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String, Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32, 8);
            DataTable dt = DB.ExecuteDataSet(dbCommand).Tables[0];

            TotalCount = Convert.ToInt32(DB.GetParameterValue(dbCommand, "TotalCount"));
            return dt;
        }

        public static DataTable GetTJReport(DateTime KQDateFrom, DateTime KQDateTo)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("XZKaoQin_GetTJReport", KQDateFrom, KQDateTo);
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }
    }
}
